/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPShowAusleihe.java
 *
 * Created on 14.12.2011, 15:57:18
 */
package de.haw.dbp.a4.gui;

import dc.connectionmanager.database.DBConnectionManager;
import dc.connectionmanager.logic.ICallback;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author jmenzel
 */
public abstract class ADBListPanel extends javax.swing.JPanel 
{
    protected final MainFrame parent;
    protected final DBConnectionManager dbcm;

    /** Creates new form JPShowAusleihe */
    public ADBListPanel(MainFrame rParent, DBConnectionManager rDB) 
    {
        super();
        this.parent = rParent;
        this.dbcm = rDB;
        
        initComponents();
        
        this.loadDefaultData(new ICallback() 
        {

            @Override
            public void handleResult(Object o) 
            {
                if(o != null)
                {
                    fillTable((ArrayList<ArrayList<String>>) o);
                }
            }
        });
        
        this.loadFilterCombobox(new ICallback() 
        {

            @Override
            public void handleResult(Object result)
            {
                if(result != null)
                {
                    fillFilterCombobox((ArrayList<ArrayList<String>>) result);
                }
            }
        });
        
        jtf_quicksearch.getDocument().addDocumentListener(new DocumentListener() 
        {
            @Override
            public void insertUpdate(DocumentEvent e)
            {
                reloadData();
            }

            @Override
            public void removeUpdate(DocumentEvent e)
            {
                reloadData();
            }

            @Override
            public void changedUpdate(DocumentEvent e)
            {
                reloadData();
            }
        });
        
        cb_filter.addItemListener( new ItemListener() 
        {

            @Override
            public void itemStateChanged(ItemEvent e)
            {
                reloadData();
            }
        });
        
        cb_filter2.addItemListener( new ItemListener() 
        {
            @Override
            public void itemStateChanged(ItemEvent e)
            {
                reloadData();
            }
        });
    }
    
    public void reloadData()
    {
        loadDataWithFilter(new ICallback() 
        {

            @Override
            public void handleResult(Object result)
            {
                if(result != null)
                {
                    fillTable((ArrayList<ArrayList<String>>)result);
                }
            }
        });
    }
    
    protected abstract void loadDataWithFilter(ICallback cb);
    protected abstract void loadDefaultData(ICallback cb);
    protected abstract void loadFilterCombobox(ICallback cb);
    
    protected void fillTable(ArrayList<ArrayList<String>> data)
    {
        try
        {
            this.jt_dataTable.removeAll();
            this.jt_dataTable.updateUI();

            this.jt_dataTable.setModel(new DefaultTableModel());
            DefaultTableModel model = ((DefaultTableModel)this.jt_dataTable.getModel());

            if(data.size() > 0)
            {
                model.setColumnCount(data.get(0).size());
                model.setColumnIdentifiers(data.get(0).toArray());


                for(ArrayList<String> list : data)
                {
                    if(list != data.get(0))
                    {
                        if(list != null)
                        {
                            model.addRow(list.toArray());
                        }
                    }
                }
            }
            model.fireTableDataChanged();
        }
        catch(Exception e)
        { }
    }
    
    private void fillFilterCombobox(ArrayList<ArrayList<String>> data)
    {
        cb_filter.removeAllItems();
        cb_filter.addItem("Kein Filter");
        if(data.size() > 0)
        {
            for(ArrayList<String> list : data)
            {
                if(list != data.get(0))
                {
                    cb_filter.addItem(list.get(0));
                }
                
            }
        }
    }
    
    protected abstract void l_addItemOnClick(MouseEvent evt);

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jsp_tableContainer = new javax.swing.JScrollPane();
        jt_dataTable = new javax.swing.JTable();
        jp_controlPanel = new javax.swing.JPanel();
        jtf_quicksearch = new javax.swing.JTextField();
        l_filter = new javax.swing.JLabel();
        cb_filter = new javax.swing.JComboBox();
        cb_filter2 = new javax.swing.JComboBox();
        l_addItem = new javax.swing.JLabel();

        jt_dataTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jsp_tableContainer.setViewportView(jt_dataTable);

        l_filter.setText("Filter:");

        l_addItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/de/haw/dbp/a4/gui/images/add.png"))); // NOI18N
        l_addItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                l_addItemMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jp_controlPanelLayout = new javax.swing.GroupLayout(jp_controlPanel);
        jp_controlPanel.setLayout(jp_controlPanelLayout);
        jp_controlPanelLayout.setHorizontalGroup(
            jp_controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jp_controlPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(l_filter)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtf_quicksearch, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cb_filter, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cb_filter2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 110, Short.MAX_VALUE)
                .addComponent(l_addItem)
                .addContainerGap())
        );
        jp_controlPanelLayout.setVerticalGroup(
            jp_controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jp_controlPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jp_controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(l_addItem, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jp_controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(l_filter)
                        .addComponent(jtf_quicksearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cb_filter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cb_filter2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jp_controlPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jsp_tableContainer, javax.swing.GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jp_controlPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jsp_tableContainer, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void l_addItemMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_l_addItemMouseClicked
    {//GEN-HEADEREND:event_l_addItemMouseClicked
        this.l_addItemOnClick(evt);
    }//GEN-LAST:event_l_addItemMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JComboBox cb_filter;
    protected javax.swing.JComboBox cb_filter2;
    protected javax.swing.JPanel jp_controlPanel;
    protected javax.swing.JScrollPane jsp_tableContainer;
    protected javax.swing.JTable jt_dataTable;
    protected javax.swing.JTextField jtf_quicksearch;
    protected javax.swing.JLabel l_addItem;
    private javax.swing.JLabel l_filter;
    // End of variables declaration//GEN-END:variables
}
